package com.luhongda.music.utils;

import cn.hutool.crypto.SecureUtil;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3FileReader;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.TagException;

import java.io.File;
import java.io.IOException;

// http://mobilecdn.kugou.com/api/v3/search/song?format=json&keyword=真的爱你&page=1
public class MusicUtils {

    public static void main(String[] args) throws TagException, InvalidAudioFrameException, ReadOnlyFileException, IOException {
        File file = new File("C:\\Users\\63461\\Downloads\\毒苹果-G.E.M.邓紫棋.mp3");
        md5("C:\\Users\\63461\\Downloads\\毒苹果-G.E.M.邓紫棋.mp3");
        MP3FileReader reader = new MP3FileReader();
        AudioFile audioFile = reader.read(file);
        System.out.println(audioFile.toString());
        System.out.println(" ----- ----- ");
        MP3AudioHeader audioHeader = new MP3AudioHeader(file);
        System.out.println("标题:" + audioFile.getTag().getFirst(FieldKey.TITLE));
        System.out.println("作者:" + audioFile.getTag().getFirst(FieldKey.ARTIST));
        System.out.println("专辑:" + audioFile.getTag().getFirst(FieldKey.ALBUM));
        System.out.println("比特率:" + audioHeader.getBitRate());
        System.out.println("时长:" + audioHeader.getTrackLengthAsString() + " (" + audioHeader.getTrackLength() + "s)");
        System.out.println("大小:" + (audioFile.getFile().length() / 1024F / 1024F) + "MB");
        System.out.println(" ----- ----- ");


    }

    public static String md5(String path) {
        return SecureUtil.md5().digestHex(path);
    }
}
